#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : __init__
# @IDE            : PyCharm
# @desc           : 描述

from datetime import datetime
from fastapi import APIRouter, Depends, Request
from utils.response import SuccessResponse
from . import schemas, crud, params, models
from core.dependencies import IdList
from apps.base.auth.validate import AllUserAuth, Auth
from apps.check.feature.crud import ASRFeatureDal
from apps.check.param.crud import ParamDal
from sqlalchemy.orm import joinedload
from core.exception import CustomException


app = APIRouter()

###################################################
#     对接基础数据，可派工人员列表                  #
###################################################
@app.get("/material", summary="物料列表")
async def get_emp_teams(
        request: Request,
        p: params.MaterialParams = Depends(),
        auth: Auth = Depends(AllUserAuth()),
):
    datas, count = await crud.QARFemaleDal(auth.db).get_material(headers=request.headers, **p.dict())
    return SuccessResponse(datas, count=count)


###################################################
#                    QAR母本操作                   #
###################################################
@app.get("/female", summary="获取QAR母本表单")
async def get_female(
        p: params.FemaleParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.QARFemaleDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.QARFemaleSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/female", summary="创建QAR母本表单")
async def create_female(
        data: schemas.QARFemaleIn,
        auth: Auth = Depends(AllUserAuth())
):
    data.create_user = auth.user.user_str
    return SuccessResponse(await crud.QARFemaleDal(auth.db).create_data(data))


@app.put("/female/{data_id}", summary="更新QAR母本表单")
async def put_female(
        data_id: int,
        data: schemas.QARFemaleIn,
        auth: Auth = Depends(AllUserAuth())

):
    data.update_user = auth.user.user_str
    return SuccessResponse(await crud.QARFemaleDal(auth.db).put_data(data_id, data=data))


@app.put("/female/{data_id}/version", summary="升版QAR母本表单")
async def put_female_version(
        data_id: int,
        auth: Auth = Depends(AllUserAuth())
):
    obj = await crud.QARFemaleDal(auth.db).get_data(data_id=data_id)
    if obj.status != "启用":
        raise CustomException("未启用母本不允许升版，请先启用")
    obj.update_user = auth.user.user_str
    obj.version = obj.version + 1
    obj.status = '升版中'
    await crud.QARFemaleDal(auth.db).flush(obj)
    return SuccessResponse('升版成功')


@app.get("/female/{data_id}", summary="获取QAR母本表单详情")
async def get_female_detail(
        data_id: int,
        auth: Auth = Depends(AllUserAuth())
):
    return SuccessResponse(await crud.QARFemaleDal(auth.db).get_data(data_id=data_id, v_schema=schemas.QARFemaleOut))


@app.delete("/female", summary="批量删除QAR母本表单", description="软删除")
async def delete_female(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.QARFemaleDal(auth.db).delete_datas(ids=ids.ids, v_soft=True, delete_user=auth.user.user_str)
    return SuccessResponse("删除成功")


############### 检验项目 ####################
# @app.get("/female/{female_id}/content", summary="获取QAR检验项目")
# async def get_female_content(
#         female_id: int,
#         auth: Auth = Depends(AllUserAuth())
# ):
#     datas, count = await crud.QARFemaleContentDal(auth.db).get_datas(
#         qar_female_id=female_id,
#         v_return_count=True
#     )
#     return SuccessResponse(datas, count=count)


# @app.post("/female/{female_id}/content", summary="创建QAR检验项")
# async def create_female_content(
#         female_id: int,
#         data: schemas.QARFemaleContent,
#         auth: Auth = Depends(AllUserAuth())
# ):
#     obj = await crud.QARFemaleDal(auth.db).get_data(data_id=female_id)
#     if obj.status == "启用":
#         raise CustomException("已启用母本不允许编辑，请去升版")

#     data.create_user = auth.user.user_str
#     return SuccessResponse(await crud.QARFemaleContentDal(auth.db).create_data(data))


# @app.put("/female/{female_id}/content/{data_id}", summary="更新QAR检验项")
# async def put_female_content(
#         female_id: int,
#         data_id: int,
#         data: schemas.QARFemale,
#         auth: Auth = Depends(AllUserAuth())

# ):
#     obj = await crud.QARFemaleDal(auth.db).get_data(data_id=female_id)
#     if obj.status == "启用":
#         raise CustomException("已启用母本不允许编辑，请去升版")
    
#     data.update_user = auth.user.user_str
#     return SuccessResponse(await crud.QARFemaleDal(auth.db).put_data(data_id, data=data))


# @app.delete("/female/{female_id}/content", summary="批量删除QAR检验项", description="硬删除")
# async def delete_female_content(
#     female_id: int,
#     ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())
# ):
#     obj = await crud.QARFemaleDal(auth.db).get_data(data_id=female_id)
#     if obj.status == "启用":
#         raise CustomException("已启用母本不允许编辑，请去升版")

#     await crud.QARFemaleDal(auth.db).delete_datas(ids=ids.ids, v_soft=False)
#     return SuccessResponse("删除成功")